<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Student Registration - Teaching Management System</title>
    <link rel="stylesheet" href="account-login.css">
</head>
<body>
    <div class="container">
        <h2>create an account</h2>
        <form id="register-form">
            <div class="input-group">
                <label for="userid">Student ID</label>
                <input type="text" id="userid" placeholder="Please enter your student ID" required>
                <span id="us-error-message" class="error-message"></span>
            </div>
            
            <div class="input-group">
                <label for="phone">cell-phone number</label>
                <input type="tel" id="phone" placeholder="Please enter 11-digit phone number" required>
                <span id="ph-error-message" class="error-message"></span>
            </div>
            
            <div class="input-group">
                <label for="password">password</label>
                <input type="password" id="password" placeholder="6-16 digits and letters combination" required>
                <span id="pa-error-message" class="error-message"></span>
            </div>
            
            <div class="input-group">
                <label for="confirm-password">Confirm password</label>
                <input type="password" id="confirm-password" placeholder="Please enter the password again" required>
                <span id="cp-error-message" class="error-message"></span>
            </div>
            
            <button id="btn" type="button" class="button">
                <span class="button-text">Register Now</span>
            </button>
            
            <div id="success-message" class="success-message"></div>
        </form>
        
        <div class="auth-links">
            <p>
                Do you already have an account?<a href="login.html">Login Now</a>
            </p>
        </div>
    </div>
    
    <!-- 登录中加载覆盖层 -->
    <!-- 遮罩层 -->
        <div id="loading-overlay">
        <div class="loading-box">
            <div class="spinner"></div>
            <p>Registering</p>
        </div>
        </div>
    
    <script src="../database.js"></script>
    <script src="account.js"></script>
    <script>
          const overlay = document.getElementById("loading-overlay");
            
        function showLoading() {
        document.getElementById("loading-overlay").classList.add("show");
        }
        function hideLoading() {
        document.getElementById("loading-overlay").classList.remove("show");
        }

        // Check if registration is enabled
        window.onload = function() {
            const teacher = JSON.parse(localStorage.getItem('teacher'));
            if(!teacher || !teacher.allowRegister) {
                alert('Registration is closed, please contact the teacher');
                window.location.href = 'login.html';
            }
            hideLoading(); // Ensure the loading overlay is hidden when entering the page
        }

        // When leaving the user ID input field, verify if the user ID is filled
        document.getElementById('userid').addEventListener('blur', () => {
            checkUserid();
        });

        // When leaving the phone number input field, verify if the phone number is filled
        document.getElementById('phone').addEventListener('blur', () => {
            checkPhone();
        });

        // When leaving the password input field, verify if the password is filled
        document.getElementById('password').addEventListener('blur', () => {
            checkPassword();
        });

        // When leaving the confirm password input field, verify if the confirm password is filled
        document.getElementById('confirm-password').addEventListener('blur', () => {
            checkConfirmPassword();
        });

        // When clicking the register button, verify if the password and confirm password match
        document.getElementById('btn').addEventListener('click', () => {
            const button = document.getElementById('btn');
            const buttonText = button.querySelector('.button-text');
            const userid = document.getElementById('userid').value;
            const phone = document.getElementById('phone').value;
            let password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirm-password').value;

            // Verify if the information is filled
            if(!checkUserid() || !checkPhone() || !checkPassword() || !checkConfirmPassword()) {
                return;
            }
            showLoading();

            // Show loading state
            // button.classList.add('loading');
            // button.disabled = true;
            // buttonText.textContent = 'Registering...';

            // Simulate network delay
            setTimeout(() => {
                // Check if the user ID already exists
                const item = JSON.parse(localStorage.getItem(userid));
                if (item) {
                    document.getElementById('us-error-message').textContent = 'This student ID has been registered';
                    hideLoading();
                    return;
                }
                document.getElementById('us-error-message').textContent = '';

                // Verify if the password and confirm password match
                if (password !== confirmPassword) {
                    document.getElementById('cp-error-message').textContent = 'The two passwords do not match, please enter again';
                    hideLoading();
                    return;
                }
                document.getElementById('cp-error-message').textContent = '';

                // Add salt hash encryption to the password
                password = simpleHash(password, userid);

                // Interact with localStorage, store user information in localStorage
                const user = {
                    userid: userid,
                    username: userid, // Username defaults to userid
                    phone: phone,
                    password: password,
                    avatar: '../header-footer/images/ico.jpg', // Avatar URL
                    identity: 'student', // Student
                    gender: 'Not disclosed', // Gender
                    status: true, // Status: Enabled/Disabled
                }
                localStorage.setItem(user.userid, JSON.stringify(user)); // userid as key

                // Clear error messages
                document.getElementById('us-error-message').textContent = '';
                document.getElementById('ph-error-message').textContent = '';
                document.getElementById('pa-error-message').textContent = '';
                document.getElementById('cp-error-message').textContent = '';

                // Show registration success message
                // document.getElementById('success-message').textContent = 'Registration successful！Redirecting to login page...';

                // Redirect to login page after 2s
                setTimeout(() => {
                    window.location.href = 'login.html';
                }, 2000);
            }, 1000);
        });

        // Reset button state
        // function resetButtonState() {
        //     const button = document.getElementById('btn');
        //     const buttonText = button.querySelector('.button-text');
        //     button.classList.remove('loading');
        //     button.disabled = false;
        //     buttonText.textContent = 'Register Now';
        // }

        // Add input field animation effect
        document.querySelectorAll('.input-group input').forEach(input => {
input.addEventListener('focus', function() {
this.parentElement.classList.add('focused');
});
input.addEventListener('blur', function() {
if (!this.value) {
this.parentElement.classList.remove('focused');
}
});
});
//Real time verification of password matching
document.getElementById('confirm-password').addEventListener('input', function() {
const password = document.getElementById('password').value;
const confirmPassword = this.value;
const errorElement = document.getElementById('cp-error-message');
if (confirmPassword && password !== confirmPassword) {
errorElement.textContent='Password mismatch'; // Fixed variable name from ErrElement to errorElement
} else {
errorElement.textContent = '';
}
});
    </script>
</body>
</html>